*==============================================================================*
*    Cumulative Share of Fleet or Cohort Emissions from each Vehicle Age       *
*==============================================================================*
u "dataSTATA/combined/combined_smogcheck_colorado.dta", clear
keep if age >= 4
keep if test_year == 2014

* age in 2-year categories
replace age = 4  if age == 5
replace age = 6  if age == 7
replace age = 8  if age == 9
replace age = 10 if age == 11
replace age = 12 if age == 13
replace age = 14 if age == 15
replace age = 16 if age == 17
replace age = 18 if age == 19
replace age = 20 if age == 21
replace age = 22 if age == 23
replace age = 24 if age == 25
replace age = 26 if age == 27
replace age = 28 if age == 29
replace age = 30 if age >= 31 & age < .


* age=4 really covers 5.5 years (ages 0-5, assuming age=0 is half year)
g       vmtPerPeriod = annualVmt * 2 if age != 4
replace vmtPerPeriod = annualVmt * 5.5 if age == 4
g total_CO  = emissions_used_CO  * vmtPerPeriod
g total_HC  = emissions_used_HC  * vmtPerPeriod
g total_NOX = emissions_used_NOX * vmtPerPeriod
g total_CO2 = emissions_used_CO2 * vmtPerPeriod
g total_vmt = vmtPerPeriod
collapse (sum) total_*, by(age) fast
egen Ttotal_CO  = total(total_CO)
egen Ttotal_HC  = total(total_HC)
egen Ttotal_NOX = total(total_NOX)
egen Ttotal_CO2 = total(total_CO2)
egen Ttotal_vmt = total(total_vmt)
g sh_CO  = total_CO  / Ttotal_CO
g sh_HC  = total_HC  / Ttotal_HC
g sh_NOX = total_NOX / Ttotal_NOX
g sh_CO2 = total_CO2 / Ttotal_CO2
g sh_vmt = total_vmt / Ttotal_vmt
g cdf_CO  = sum(sh_CO)
g cdf_HC  = sum(sh_HC)
g cdf_NOX = sum(sh_NOX)
g cdf_CO2 = sum(sh_CO2)
g cdf_vmt = sum(sh_vmt)

tw  (connected cdf_CO age, msymb(C)) ///
	(connected cdf_HC age, msymb(S) lpattern(dash)) ///
	(connected cdf_NOX age, msymb(Dh) lpattern(shortdash)) ///
	(line cdf_CO2 age, lpattern(longdash)), ///
	graphr(color(white)) ///
	yscale(noline) ///
	ytit("Cumulative share of lifetime pollution") ///
	xtit("Age (Years)") ///
	xline(10 15) ///
	ylab(0 0.25 0.5 0.75 1) ///
	xlab(4 8 12 16 20 24 30) ///
	legend(order(1 "Carbon monoxide (CO)" 2 "Hydrocarbons (HC)" 3 "Nitrogen oxides (NOx)" 4 "Carbon dioxide (CO2)")) ///
	saving("results/figures/f7_cdfs/panelA.gph", replace)
graph export "results/figures/f7_cdfs/panelA.wmf", replace
graph export "$overleaf/figures/f7_cdfs/panelA.eps", replace


tw  (connected cdf_CO age, msymb(C)) ///
	(connected cdf_HC age, msymb(S) lpattern(dash)) ///
	(connected cdf_NOX age, msymb(Dh) lpattern(shortdash)) ///
	(line cdf_CO2 age, lpattern(longdash)) ///
	(line cdf_vmt age, lpattern(longdash)), ///
	graphr(color(white)) ///
	yscale(noline) ///
	ytit("Cumulative share of lifetime pollution") ///
	xtit("Age (Years)") ///
	xline(10 15) ///
	ylab(0 0.25 0.5 0.75 1) ///
	xlab(4 8 12 16 20 24 30) ///
	legend(order(1 "Carbon monoxide (CO)" 2 "Hydrocarbons (HC)" 3 "Nitrogen oxides (NOx)" 4 "Carbon dioxide (CO2)" 5 "Vehicle miles traveled")) ///
	saving("results/figures/f7_cdfs/panelA_withvmt.gph", replace)
//graph export "results/figures/f7_cdfs/panelA_withvmt.wmf", replace
//graph export "$overleaf/figures/f7_cdfs/panelA_withvmt.eps", replace

list age cdf*



